// pages/person/address/address.js
const req = require('../../../utils/req.js');
const {
  api
} = require('../../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifmask: false,
    addresslist:[],
    province: [], //省份选择器列表
    pro_index: 0, //省份选择器下标
    province_id: null, //省份id
    city: [], //市级选择器列表
    city_index: 0, //市级选择器下标
    city_id: null, //市级id
    district: [], //区级选择器列表
    district_index: 0, //区级选择器下标
    district_id: null, //区级id
    name:'',
    mobile:'',
    address:'',
    is_default:0,
    ifedit:null,

  },
  //不做任何事,阻止冒泡用
  nothing() { },
  //点击遮罩空白处关闭
  hidden(e){
    this.setData({
      ifmask: false,
    })
   this.emptyinput();
  },
  //清空输入数据
  emptyinput(){
    this.setData({
      province_id: null,
      city_id: null,
      district_id: null,
      name: '',
      mobile: '',
      address: '',
      is_default: 0,
      ifedit: null,
      city: [],
      district: [],
    })
  },
  // 点击地址进入编辑
  edit(e){
    // console.log(e.currentTarget.dataset.info);
    let info = e.currentTarget.dataset.info;
    this.setData({
      province_id: info.province_id,
      pro_index: info.province_id-2,
      city_id: info.city_id,
      district_id: info.district_id,
      name:info.name,
      mobile: info.mobile,
      address: info.address,
      is_default: info.is_default,
      ifedit:info.id,
    })
    this.getcity();
    this.getdistrict();
    this.setData({
      ifmask: true,
    })
  },
  //新增地址
  address_add() {
    this.setData({
      ifmask: true,
    })
  },
  //删除地址
  address_del(e){
    console.log(e)
    var that=this;
    wx.showModal({
      title: '警告',
      content: '是否删除此地址?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let { address_delete } = api;
          address_delete.data.id = e.currentTarget.dataset.id;
          req.ask(address_delete).then(res => {
            console.log(res)
            that.getlist();
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },
  //省份选择器
  picker_province(e) {
    var that = this;
    console.log(e)
    // 1. 根据 index 获取 id
    let province_id = this.data.province[e.detail.value].id;
    this.setData({
      pro_index: e.detail.value,
      district: [],
      province_id: province_id,
    })
    // 2, 根据 id 获取市级列表
    this.getcity();
  },
  //获取市
  getcity(){
    let {
      region_list
    } = api;
    region_list.data.parentId = this.data.province_id;
    req.ask(region_list).then(res => {
      let result = res.data.data
      this.setData({
        city: result
      })
    })
  },
  //市级选择器
  picker_city(e) {
    var that = this;
    console.log(e)
    // 1. 根据 index 获取 id
    let city_id = this.data.city[e.detail.value].id;
    this.setData({
      city_index: e.detail.value,
      city_id: city_id,
    })
    // 2, 根据 id 获取区级列表
    this.getdistrict();
  },
  //获取区
  getdistrict(){
    let {
      region_list
    } = api;
    region_list.data.parentId = this.data.city_id;
    req.ask(region_list).then(res => {
      let result = res.data.data
      this.setData({
        district: result
      })
    })
  },
  //区级选择器
  picker_district(e) {
    var that = this;
    console.log(e)
    // 1. 根据 index 获取 id
    let district_id = this.data.district[e.detail.value].id;
    this.setData({
      district_index: e.detail.value,
      district_id: district_id
    })
  },
  //表单确认
  formSubmit(e) {
    var that=this;
    // 1. 获取页面的值
    e.detail.value.province_id = this.data.province_id;
    e.detail.value.city_id = this.data.city_id;
    e.detail.value.district_id = this.data.district_id;
    let objaddress = JSON.stringify(e.detail.value);
    //2. 调用接口保存地址
    let {
      address_save
    } = api;
    address_save.data = JSON.parse(objaddress);
    address_save.data.id = that.data.ifedit;
    console.log(address_save.data)
    req.ask(address_save).then(res => {
      console.log('address_save', res)
      this.getlist();
      this.setData({
        ifmask: false,
      })
      this.emptyinput();
    })
  },

  //获取地址列表
  getlist() {
    var that = this;
    let {
      address_list,
    } = api;
    req.ask(address_list).then(res => {
      console.log(res)
      that.setData({
        addresslist:res.data.data,
      })
    })

  },

  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    this.getlist();
    let {region_list} = api;
    req.ask(region_list).then(res => {
      this.setData({
        province: res.data.data
      })
    })
  },

  holdData(){
    wx.navigateTo({
      url: '../../cart/order/order',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShdistrictppMessage: function() {

  }
})